<?php
/*
 * @Author: lokei
 * @Date: 2022-09-12 00:02:07
 * @LastEditors: lokei
 * @LastEditTime: 2023-10-10 17:56:25
 * @Description: 
 */
namespace App\Http\Controllers\Store\Statistic;

use App\Common\Permission\RoleGuard;
use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Store\StoreModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class StatisticController extends Controller {
    public function all() {
        $res = ResultTool::success();
        $model = RoleGuard::store_guard(null, 'App\Models\Store\StoreModel', 'id');
        if(is_null($model)) {
            $store_list = StoreModel::get();
        } else {
            $store_list = $model->get();
        }
        $cur_time = time();
        $day = date('Ymd', time());
        $today_time = strtotime($day . ' 00:00:00');
        $expire = 24*60*60 - ($cur_time - $today_time);
        $items = array();
        foreach($store_list as $store) {
            $item['store_name'] = $store->name;
            $key = 'store_statistic_' . (config('app.proj_identity') ? (config('app.proj_identity') . '_') : '') . $store->id . '_' . $day;
            if (Redis::exists($key)) {
                $item['statistic'] = Redis::hGetAll($key);
            } else {
                Redis::hMset($key,['order_total' => 0, 'order_payed' => 0, 'money_total' => 0.0, 'money_wx' => 0.0, 'money_balance' => 0.0]);
                Redis::expire($key, $expire);
                $item['statistic'] = Redis::hGetAll($key);
            }
            array_push($items, $item);
        }
        $res['data']['items'] = $items;
        return $res;
    }

    public function incPv(Request $request) {
        $store = StoreModel::where('id', '=', $request->input('store_id'))->first();
        $store->pv = $store->pv + 1;
        $store->save();
        return ResultTool::success();
    }
}